package com.lzl.service.impl;

import com.lzl.entity.StorageRecord;
import com.lzl.mapper.StorageRecordMapper;
import com.ktjiaoyu.service.StorageRecordService;
import com.lzl.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("storageRecordService")
public class StorageRecordServiceImpl implements StorageRecordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StorageRecordServiceImpl.class);

    @Autowired
    private StorageRecordMapper storageRecordMapper;

    @Override
    public Page<StorageRecord> getPage(StorageRecord entity, Integer pageNo, Integer pageSize) {
        Page<StorageRecord> page = null;
        try{
            page  = new Page<StorageRecord>();
            page.setPageSize(pageSize);
            //查询总数，计算总页数
            Long count = storageRecordMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
            //查询分页数据
            List<StorageRecord> data = storageRecordMapper.getPage(entity,page.offset(),page.getPageSize());
            page.setData(data);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.getPage(StorageRecord,Integer,Integer)方法出现异常.",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(StorageRecord entity) throws Exception {
        try{
            if(entity.getId() == null){
                //无主键则新增
                return storageRecordMapper.insert(entity) > 0;
            }else{
                //有主键则修改
                return storageRecordMapper.update(entity) > 0;
            }
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.saveOrUpdate(SysUser)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public StorageRecord findById(Long id) throws Exception {
        try{
            return storageRecordMapper.getById(id);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.findById(Long)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public StorageRecord findBySrCode(String srCode) throws Exception {
        try{
            return storageRecordMapper.getBySrCode(srCode);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.findByAccount(String)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public boolean deleteById(Long id) throws Exception {
        try{
            return storageRecordMapper.delete(id) > 0;
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.deleteById(Long)方法出现异常.",e);
            throw e;
        }
    }

}
